<%@ page import="java.util.Arrays" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="Model.Student" %>
<%@ page import="Dao.jdbcTools" %>
<%@ page import="java.util.List" %>
<%@ page import="java.io.InputStream" %>
<%@ page import="org.apache.ibatis.io.Resources" %>
<%@ page import="org.apache.ibatis.session.SqlSessionFactory" %>
<%@ page import="org.apache.ibatis.session.SqlSessionFactoryBuilder" %>
<%@ page import="org.apache.ibatis.session.SqlSession" %>
<%@ page import="Dao.StudentDao" %><%--
  Created by IntelliJ IDEA.
  User: 86182
  Date: 2024/8/1
  Time: 15:47
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>学生记录分页演示</title>
    <style>
      /* Border styles */
    #table-4 thead, #table-4 tr {
      border-top-width: 1px;
      border-top-style: solid;
      border-top-color: rgb(211, 202, 221);
    }
    #table-4 {
      border-bottom-width: 1px;
      border-bottom-style: solid;
      border-bottom-color: rgb(211, 202, 221);
    }

    /* Padding and font style */
    #table-4 td, #table-4 th {
      padding: 5px 10px;
      font-size: 12px;
      font-family: Verdana;
      color: rgb(95, 74, 121);
    }

    /* Alternating background colors */
    #table-4 tr:nth-child(even) {
      background: rgb(223, 216, 232)
    }
    #table-4 tr:nth-child(odd) {
      background: #FFF
    }
  </style>
</head>
<body>
<a href="loginout">退出系统</a>
<%
  InputStream in = Resources.getResourceAsStream("config/mybatis-config.xml");
  SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
  SqlSession sqlSession = factory.openSession();
  StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
  List<Student> list1 = studentDao.findAll();
  //List<Student> list1 = jdbcTools.selectall("select * from stuhhh", Student.class);
  //1.设定每一页的记录数
  int pageSize=3;
  //2.计算总的记录数
  int recordCountTotal = list1.size();
  //3.分页的总页数
  int pageCount = (int) Math.ceil(recordCountTotal * 1.0/ pageSize);
  System.out.println("pageCount = " + pageCount);
  int curPage=1;
  if (request.getParameter("curPage")==null){
    curPage=1;
  }else {
      curPage = Integer.valueOf(request.getParameter("curPage"));
  }
  //5.构建sql语句查询当前页的记录数
  String sql=" select * from stuhhh limit " + ( curPage - 1 ) * pageSize + "," + pageSize;
  //6.获取当前页数据
  //List<Student> list = studentDao.findAll1(sql);
  List<Student> list = jdbcTools.selectall(sql, Student.class);
%>
<div class="#table-4 tr:nth-child(ood)" style="text-align: center">
<h1 style="text-align: center">学生信息展示</h1>
  <h3><a href='insert'>录入信息</a></h3>
<table id="table-4" cellpadding='3' cellspacing='0' border='1' align='center'>
  <tr id="table-4 tr">
    <th id="table-4 th">学号</th>
    <th>姓名</th>
    <th>性别</th>
    <th>年龄</th>
    <th>专业</th>
    <th>时间</th>
    <th>操作1</th>
    <th>操作2</th>
  </tr>
  <%
    for (int i = 0; i < list.size(); i++) {
  %>
  <tr id="table-4 tr">
    <td id="table-4 td"><%=list.get(i).getId()%></td>
    <td><%=list.get(i).getName()%></td>
    <td><%=list.get(i).getSex()%></td>
    <td><%=list.get(i).getAge()%></td>
    <td><%=list.get(i).getMajor()%></td>
    <td><%=list.get(i).getTime()%></td>
    <td onclick="if(confirm('确认删除吗？')) return true;return false;methon='post'"><a href=delOne?id=<%=list.get(i).getId()%>>删除</a></td>
    <td><a href=updateOne?id=<%=list.get(i).getId()%>>更新</a></td>
  </tr>
  <%
    }
  %>
</table>
  <%
    if (curPage == 1){
  %>
  <span class="nice">首页</span>
  <span>上一页</span>
  <a href="showlist4.jsp?curPage=<%=curPage+1%>">下一页</a>
  <a href="showlist4.jsp?curPage=<%=pageCount+1%>">尾页</a>
   <%
     }else if (curPage == pageCount){
   %>
  <a href="showlist4.jsp?curPage=1">首页</a>
  <a href="showlist4.jsp?curPage=<%=curPage-1%>">上一页</a>
  <span class="nice">下一页</span>
  <span>尾页</span>
  <%
     }else {
  %>
  <a href="showlist4.jsp?curPage=1">首页</a>
  <a href="showlist4.jsp?curPage=<%=curPage-1%>">上一页</a>
  <a href="showlist4.jsp?curPage=<%=curPage+1%>">下一页</a>
  <a href="showlist4.jsp?curPage=<%=pageCount%>">尾页</a>
  <%
     }
   %>
</div>
</body>
</html>
